package hai.wom.org.qyer_home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import hai.wom.org.qyer_home.R;
import hai.wom.org.qyer_home.adapter.MyAdapter_FgItem;
import hai.wom.org.qyer_home.bean.Slide_data;
import hai.wom.org.qyer_home.callback.ICallback;
import hai.wom.org.qyer_home.utils.HttpUtils;

/**
 * Created by Administrator on 2017/1/11 0011.
 */

public class Fragment_Free extends Fragment {
    GridView gv;
MyAdapter_FgItem adapter;
    List<Slide_data.DataEntity.BarEntity> list;
    private String path = "http://open.qyer.com/lastminute/home/major?client_id=qyer_discount_androi&client_secret=227097da1d07a2a9860f&track_user_id=&track_deviceid=352284042226724&track_app_version=2.0.4&track_app_channel=qq&track_device_info=hlte&track_os=Android4.4.2&app_installtime=1483531797801&size=720x1242&ra_referer=app_home&lat=39.084881&lon=117.199403&oauth_token=";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fg_layout, container, false);
        gv = (GridView) view.findViewById(R.id.gv_fragment);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list=new ArrayList<>();
        adapter=new MyAdapter_FgItem(list,getContext());
        gv.setAdapter(adapter);
        new HttpUtils(new ICallback() {
            @Override
            public void getData(String result) {
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    JSONObject data=jsonObject.getJSONObject("data");
                    JSONArray bar=data.getJSONArray("bar");
                    for (int i = 0; i < 8; i++) {
                        JSONObject js=bar.getJSONObject(i);
                        Slide_data.DataEntity.BarEntity abar=new Slide_data.DataEntity.BarEntity();
                        abar.setName(js.getString("name"));
                        abar.setIcon(js.getString("icon"));
                        list.add(abar);
                    }
                    Log.e("Bar",""+list.size());
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).execute(path);
    }

}
